﻿using HardcoreGame.Common.Helpers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class LuminiteFragment : ModProjectile
    {
        private float rotationSpeed = 0.2f; 
        private float currentRotation = 0f;
        public override void SetDefaults()
        {
            Projectile.ignoreWater = false;
            Projectile.width = 16;
            Projectile.height = 16;
            Projectile.friendly = true;
            Projectile.penetrate = 1;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.tileCollide = true;
            Projectile.timeLeft = 600;
            Projectile.extraUpdates = 1;
        }
        public override bool PreDraw(ref Color lightColor)
        {
            Texture2D texture = TextureAssets.Projectile[Projectile.type].Value;
            int frameHeight = texture.Height / Main.projFrames[Projectile.type];
            Main.EntitySpriteDraw(
            texture,
            Projectile.Center - Main.screenPosition,
            new Rectangle(0, frameHeight * Projectile.frame, texture.Width, frameHeight),
            Color.White,
            currentRotation, 
            new Vector2(texture.Width / 2f, frameHeight / 2f),
            Projectile.scale,
            SpriteEffects.None,
            0
        );
            return false;
        }
        public override void AI()
        {
            currentRotation += rotationSpeed;
            if (currentRotation > MathHelper.TwoPi) currentRotation -= MathHelper.TwoPi;
            if (Projectile.timeLeft > 100)
            {
                Projectile.velocity *= 0.94f;
            }
            else 
            {
                Vector2 targetPos = Vector2.Zero;
                float minDistance = 1000f;
                bool hasTarget = false;
                foreach (NPC npc in Main.npc)
                {
                    if (npc.CanBeChasedBy())
                    {
                        float distance = Vector2.Distance(Projectile.Center, npc.Center);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            targetPos = npc.Center;
                            hasTarget = true;
                        }
                    }
                }
                if (hasTarget)
                {
                    Vector2 direction = (targetPos - Projectile.Center).SafeNormalize(Vector2.Zero);
                    float speed = Projectile.timeLeft > 50 ? 0.3f : 0.5f; 
                    Projectile.velocity = Vector2.Lerp(Projectile.velocity, direction * 15f, speed);
                }
                else
                {
                    Projectile.velocity *= 0.94f;
                }
            }
            if (Main.rand.NextBool(10) && Projectile.timeLeft < 100)
            {
                Dust.NewDust(
                    Projectile.position,
                    Projectile.width,
                    Projectile.height,
                    DustID.Vortex,
                    Scale: 0.8f
                );
            }
        }

        public override void OnKill(int timeLeft)
        {            
            for (int i = 0; i < 12; i++)
            {
                Dust dust = Dust.NewDustPerfect(
                    Projectile.Center,
                    DustID.LunarOre,
                    Vector2.One.RotatedByRandom(MathHelper.TwoPi) * Main.rand.NextFloat(2f, 4f),
                    0,
                    default,
                    1.5f
                );
                dust.noGravity = true;
            }
            SoundEngine.PlaySound(SoundID.Item89 with { Pitch = -0.2f }, Projectile.position);
        }
    }
}
